//
//  YHYSelectViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/5/4.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYSelectViewController.h"
#import "YHYAddViewCollectionViewCell.h"
#import "AppDelegate.h"
@interface YHYSelectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *mycollectionVArray;
@property (nonatomic,strong)NSMutableArray *keyArray;
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIView *backView;


@property (nonatomic,strong)UIView *myBackBigView;


@property (nonatomic,strong)UIView *shareBackGroundView;

@end

@implementation YHYSelectViewController
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCollectionView];
        _keyArray = [NSMutableArray array];
        self.mycollectionVArray = [NSMutableArray arrayWithObjects:@"纯设计",@"需要跟单",@"需要预算",@"需要施工", nil];
       
    }
    return self;
}

    
- (void)creatCollectionView{
    
    //    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    //    background.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    //    background.opaque = NO;
    //    UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
    //    [self.view addGestureRecognizer:gesture];
    //    // self.shareBackGroundView = background;
    //
    //    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //    [delegate.window addSubview:self.view];
//    _myBackBigView  = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//    _myBackBigView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
//    self.shareBackGroundView = _myBackBigView;
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [delegate.window addSubview:_shareBackGroundView];
//
//    self.myBackBigView.hidden = NO;
//    [self.myBackBigView addSubview:self];

    
    
    
    _backView = [UIView new];
   // _backView.backgroundColor = [UIColor redColor];
    [self addSubview:_backView];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom);
        make.left.mas_equalTo(self);
        make.width.mas_equalTo(self);
        
        make.height.mas_equalTo(self);
        
    }];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 7 * AUTO_WIDTH;
    

    
    
    UIButton *dismess = [[UIButton alloc]init];
    [_backView addSubview:dismess];
    [dismess mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(36/3 *AUTO_HEIGHT);
        make.right.mas_equalTo(-36/3 * AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(80/3 *AUTO_WIDTH, 80/3 * AUTO_HEIGHT));
        
    }];
    [dismess setImage:[UIImage imageNamed:@"myBack"] forState:UIControlStateNormal];
    [dismess addTarget:self action:@selector(dismessAction:) forControlEvents:UIControlEventTouchDown];
    
    
    
    self.tableView = [[UITableView alloc]init];
    [_backView addSubview:self.tableView];
    self.tableView.scrollEnabled = YES;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView.mas_top).offset(136/3 * AUTO_HEIGHTS);
        make.left.mas_equalTo(_backView);
        make.width.mas_equalTo(_backView);
        
        make.bottom.mas_equalTo(self.backView.mas_bottom).offset(0 * AUTO_HEIGHT);
    }];
    self.tableView.scrollEnabled = NO;
   // self.tableView.backgroundColor = LV;
    self.tableView.rowHeight = 178/3 * AUTO_HEIGHT;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[YHYAddViewCollectionViewCell class] forCellReuseIdentifier:@"YHYAddViewCollectionViewCell"];
    [self.tableView reloadData];
    
    
    
    
    
   }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mycollectionVArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"YHYAddViewCollectionViewCell";
    YHYAddViewCollectionViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
        cell.contentStr = [_mycollectionVArray objectAtIndex:indexPath.row];
        if (_keyArray!= nil) {
    
    
            if ([_keyArray containsObject:cell.contentStr]) {
    
                //[YHYAddViewCollectionViewCell changeColor:YES];
                [cell changeColor:YES];
            } else {
                [cell changeColor:NO];
            }
        }
        

    
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _mycollectionVArray.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YHYAddViewCollectionViewCell *cell = (YHYAddViewCollectionViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.myLabel.text = [_mycollectionVArray objectAtIndex:indexPath.row];
    NSLog(@"das");
    if ([_keyArray containsObject:cell.contentStr]) {
        // [cell changeColor:NO];
        [_keyArray removeObject:cell.contentStr];
        cell.myImageView.alpha = 0;
    }
    else{
        cell.myImageView.alpha = 1;
     [_keyArray addObject:cell.myLabel.text];
    }
    [_tableView reloadData];
}

//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    static NSString *tre = @"YHYAddViewCollectionViewCell";
//    YHYAddViewCollectionViewCell *YHYAddViewCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:tre forIndexPath:indexPath];
//    //    YHYAddViewCollectionViewCell.backgroundColor = [UIColor redColor];
//    //  YHYAddViewCollectionViewCell.backgroundColor = [UIColor whiteColor];
//    YHYAddViewCollectionViewCell.contentStr = [_mycollectionVArray objectAtIndex:indexPath.row];
//    if (_keyArray!= nil) {
//        
//        
//        if ([_keyArray containsObject:YHYAddViewCollectionViewCell.contentStr]) {
//            
//            //[YHYAddViewCollectionViewCell changeColor:YES];
//            [YHYAddViewCollectionViewCell changeColor:YES];
//        } else {
//            [YHYAddViewCollectionViewCell changeColor:NO];
//        }
//    }
//    
//    
//    return YHYAddViewCollectionViewCell;
//}
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    YHYAddViewCollectionViewCell *cell = (YHYAddViewCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.myLabel.text = [_mycollectionVArray objectAtIndex:indexPath.row];
//    if ([_keyArray containsObject:cell.contentStr]) {
//        // [cell changeColor:NO];
//        [_keyArray removeObject:cell.contentStr];
//    }
//    
//    
//    [_keyArray addObject:cell.myLabel.text];
//    [_tableView reloadData];
//    
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    return CGSizeMake((self.view.frame.size.width - 50 * 2 * AUTO_WIDTH)/3 , 40 * AUTO_HEIGHT);
//}
//
//
- (void)dismessAction:(UIButton *)btu{
    [self.mydelegate presentDiemess:_keyArray.count];
}

//- (void)wanchengAction:(UIButton *)btu{
//    [self.mydelegate presentFinish];
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
